﻿namespace AutoRevit.Extensions
{
    internal static class TaskFactoryExtensionss
    {
        /// <summary>
        /// 启动一个新任务
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="taskFactory"></param>
        /// <param name="action"></param>
        /// <param name="state"></param>
        internal static Task StartNew<T>(this TaskFactory taskFactory, Action<T> action, T state)
        {
            void taskRun(object state)
            {
                var p = (T)state;
                action(p);
            }

            return taskFactory.StartNew(taskRun!, state);
        }

        /// <summary>
        /// 启动一个新任务
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="taskFactory"></param>
        /// <param name="func"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        internal static Task<TResult> StartNew<T, TResult>(this TaskFactory taskFactory, Func<T, TResult> func, T state)
        {
            TResult taskRun(object state)
            {
                var p = (T)state;
                return func(p);
            }

            return taskFactory.StartNew(taskRun!, state);
        }
    }
}
